var hx = require("hbuilderx");
const {
	camelCase,
	paramCase,
	pascalCase,
	snakeCase,
	constantCase,
	capitalCase,
	dotCase,
	headerCase,
	noCase,
	pathCase
} = require('change-case-all');
const translatePlatforms = require('./translate/index.js')



//该方法将在插件激活的时候调用
function activate(context) {
	let disposable = hx.commands.registerCommand('extension.translation', main);

	//订阅销毁钩子，插件禁用的时候，自动注销该command。
	context.subscriptions.push(disposable);
}


function isEnglish(word) {
	return /^[a-zA-Z\d\s\/\-\._]+$/.test(word)
}

const translateCacheWords = []
async function getTranslateResult(text) {
	const engine = hx.workspace.getConfiguration('varTranslation').get('aselect') || 'baidu'
	const cache = translateCacheWords.find((item) => item.engine == engine && item.text == text)
	if (cache) {
		return cache.result
	}
	// 根据设置获取对应翻译函数
	const translate = translatePlatforms[engine]
	// 判断当前选中的文字是英文还是中文
	const language = isEnglish(text) ? 'zh' : 'en'
	try {
		const result = await translate(text, language)
		if (result) {
			translateCacheWords.push({ engine, text, result })
		}
		return result
	} catch (error) {
		hx.window.showInformationMessage(`${engine}翻译异常,请检查网络或密钥配置是否正确 ${JSON.stringify(error)}`)
		return null
	}
}

const changeCaseMap = [
	{ name: 'camelCase', handle: camelCase, description: 'camelCase 驼峰(小)' },
	{ name: 'pascalCase', handle: pascalCase, description: 'pascalCase 驼峰(大)' },
	{ name: 'snakeCase', handle: snakeCase, description: 'snakeCase 下划线' },
	{ name: 'paramCase', handle: paramCase, description: 'paramCase 中划线(小)' },
	{ name: 'headerCase', handle: headerCase, description: 'headerCase 中划线(大)' },
	{ name: 'noCase', handle: noCase, description: 'noCase 分词(小)' },
	{ name: 'capitalCase', handle: capitalCase, description: 'capitalCase 分词(大)' },
	{ name: 'dotCase', handle: dotCase, description: 'dotCase 对象属性' },
	{ name: 'pathCase', handle: pathCase, description: 'pathCase 文件路径' },
	{ name: 'constantCase', handle: constantCase, description: 'constantCase 常量' },
]


async function vscodeSelect(word) {
	const isEnglishWord = isEnglish(word)
	const items = isEnglishWord ? changeCaseMap.map((item) => ({
		label: item.handle(word),
		description: item
			.description
	})) : [
		{ label: word, description: '中文' }
	]
	const opts = {
		matchOnDescription: false,
		placeHolder: 'choose replace 选择替换',
	}
	const selections = await hx.window.showQuickPick(items, opts)
	if (!selections) {
		return
	}
	return selections.label
}



async function main() {

	let editor = await hx.window.getActiveTextEditor();
	if (!editor) {
		return
	}
	// 获取选中文字
	let document = editor.document;
	let selection = editor.selection;
	let word = document.getText(selection);

	// 拿到翻译后的结果
	const translated = await getTranslateResult(word)
	const userSelectd = await vscodeSelect(translated)
	if (!userSelectd) return
	editor.edit(editBuilder => { editBuilder.replace(selection, userSelectd) })

}


//该方法将在插件禁用的时候调用（目前是在插件卸载的时候触发）
function deactivate() {


}
module.exports = {
	activate,
	deactivate
}